package com.example.astronautcontact2206044090cxt1104;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView rvMainContact;
    private FloatingActionButton fabtnMainAdd;
    private ImageView ivMainMore;
    private ContactDAL contactDAL = new ContactDAL(this);
    private List<AstronautContact> contactList = new ArrayList<>();
    private ContactRVAdapter contactRVAdapter;
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);

            ContactDAL contactDAL = new ContactDAL(this);
            contactDAL.openDatabase();
            contactDAL.closeDatabase();
            return insets;
        });
        initView();
        fabtnMainAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = getLayoutInflater().inflate(R.layout.dialog_contact, null
                );
                new AlertDialog.Builder(MainActivity.this)
                    .setView(view)
                    .setTitle("新建联系人")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String name=((EditText)view.findViewById(R.id.edtDialogName)).getText().toString().trim();
                            String organization=((EditText)view.findViewById(R.id.edtDialogOrganization)).getText().toString().trim();
                            String phone=((EditText)view.findViewById(R.id.edtDialogPhone
                            )).getText().toString().trim();
                            String notes=((EditText)view.findViewById(R.id.edtDialogNotes
                            )).getText().toString().trim();
                            String email=((EditText)view.findViewById(R.id.edtDialogEmail
                            )).getText().toString().trim();
                            AstronautContact contact=new AstronautContact(name,phone,email,organization,notes);
                            if(contactDAL.insertContact(contact)!=-1) {
                                Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                contactList = contactDAL.queryAll();
                                contactRVAdapter.setContactList(contactList);
                                rvMainContact.getAdapter().notifyDataSetChanged();
                            }

                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                    })
                    .create()
                    .show();
            }
        });
    }
    @Override
    protected void onResume() {
            super.onResume();
            contactList = contactDAL.queryAll();
            contactRVAdapter =new ContactRVAdapter();
            contactRVAdapter.setContactList(contactList);
            rvMainContact.setLayoutManager(new LinearLayoutManager(this));
            rvMainContact.setAdapter(contactRVAdapter);
            }
    private void initView() {
        rvMainContact = (RecyclerView) findViewById(R.id.rvMainContact);
        fabtnMainAdd = (FloatingActionButton) findViewById(R.id.fabtnMainAdd);
        ivMainMore = (ImageView) findViewById(R.id.ivMainMore);
    }
}